//
//  WWSideslipViewController.m
//  WWSideslipViewControllerSample
//

#import "WWSideslipViewController.h"

@interface WWSideslipViewController ()
{
    UIButton *_backgroundBtn;
    NSString *_isfirst;
}
@end

@implementation WWSideslipViewController
@synthesize speedf,sideslipTapGes,pan;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(nitiPanAction:) name:@"pan" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(nitiShowAction:) name:@"showLeftView" object:nil];
    NSLog(@"%@",_isfirst);
    if (!_isfirst) {
        _isfirst = @"YES";
    }
    
    // Do any additional setup after loading the view.
//    _backgroundBtn = [[UIButton alloc] init];
    //[self.view addSubview:mainControl.view];
    
}

-(void)nitiPanAction:(NSNotification *)n{
    NSString *str = [n object];
    if([str isEqualToString:@"remove"]){
        NSLog(@"+++取消手势+++");
        [self MainViewRemovePan];
        _isfirst = @"NO";
        
    }else{
        NSLog(@"+++添加手势+++");
        if ([_isfirst isEqualToString:@"NO"]) {
            [mainControl.view addGestureRecognizer:pan];
        }

    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



-(instancetype)initWithLeftView:(UIViewController *)LeftView
                    andMainView:(FunctionMenuViewController *)MainView
                   andRightView:(UIViewController *)RighView
                        andBackgroundImage:(UIImage *)image;
{
    self = [super init];
    if(self){
        
        
        UIButton *leftNavBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        leftNavBtn.frame = CGRectMake(0, 0, 100, 30);
        [leftNavBtn setTitle:@"返回" forState:UIControlStateNormal];
        [leftNavBtn addTarget:self action:@selector(leftClick:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithCustomView:leftNavBtn];
        MainView.navigationItem.leftBarButtonItem = item1;
        
        speedf = 0.5;
        
        leftControl = LeftView;
        mainControl = MainView;
        righControl = RighView;
        
        UIImageView * imgview = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        [imgview setImage:image];
        [self.view addSubview:imgview];
        
        //滑动手势
        pan = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
        [mainControl.view addGestureRecognizer:pan];
        pan.direction = UISwipeGestureRecognizerDirectionRight;
//        [mainControl.view removeGestureRecognizer:pan];

        
        //单击手势
//        sideslipTapGes= [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handeTap:)];
        [sideslipTapGes setNumberOfTapsRequired:1];
        
//        [mainControl.view addGestureRecognizer:sideslipTapGes];
        
        leftControl.view.hidden = YES;
        righControl.view.hidden = YES;
        
        [self.view addSubview:leftControl.view];
        [self.view addSubview:righControl.view];
        
        [self.view addSubview:mainControl.view];
        
    }
    __weak WWSideslipViewController* mySelf = self;
    self.cb = ^(NSInteger indexPath){
        NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
        [userDefault setObject:@"NO" forKey:@"show"];
        [userDefault synchronize];
        
        
//        EvaluateController *mv = [[EvaluateController alloc]init];
//        
//        mv.cusTabView = self.cusTabView;
//        
//        VisitController *ev = [[VisitController alloc]init];
//        
//        ev.cusTabView = self.cusTabView;
//        
//        CheckController *vc = [[CheckController alloc]init];
//        
//        vc.cusTabView = self.cusTabView;
//        
//        QRCodeViewController *QRCodeVC = [[QRCodeViewController alloc]init];
//        
//        QRCodeVC.cusTabView = self.cusTabView;
//        
//        InstallController *cv = [[InstallController alloc]init];
//        MessageController *mvc = [[MessageController alloc]init];
        
        mySelf.cusTabView.hidden = YES;
        
//        cv.cusTabView = self.cusTabView;
        [mySelf MainViewRemovePan];
        
        [UIView beginAnimations:nil context:nil];
        mySelf.cusTabView.userInteractionEnabled = YES;
        mainControl.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,1.0,1.0);
        mainControl.view.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2,[UIScreen mainScreen].bounds.size.height/2);
        
        self.cusTabView.transform = CGAffineTransformScale(CGAffineTransformIdentity,1.0,1.0);
        self.cusTabView.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2,[UIScreen mainScreen].bounds.size.height-24);
        
        [UIView commitAnimations];
        
        switch (indexPath) {
            case 0:
//                [MainView pushViewController:mv animated:YES];
                NSLog(@"zheshi0");
                break;
            case 1:
//                [MainView pushViewController:ev animated:YES];
                NSLog(@"zheshi1");
                break;
            case 2:
//                [MainView pushViewController:vc animated:YES];
                NSLog(@"zheshi2");
                break;
            case 3:
//                [MainView pushViewController:cv animated:YES];
                NSLog(@"zheshi3");
                break;
            case 4:
//                [MainView pushViewController:QRCodeVC animated:YES];
                NSLog(@"zheshi4");
                break;
            case 999:
                
//                [MainView pushViewController:mvc animated:YES];
                NSLog(@"zheshi999");
            default:
                break;
                
        }
    };
//    LeftView.cb = self.cb;

    
    
    return self;
}

-(void)MainViewRemovePan{
    [mainControl.view removeGestureRecognizer:pan];
}

#pragma mark - 滑动手势

//滑动手势
- (void) handlePan: (UISwipeGestureRecognizer *)rec{

    
//    CGRect frame = rec.view.frame;
    //判断轻扫方向
    if (rec.direction ==UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"左滑");
    }
    if (rec.direction == UISwipeGestureRecognizerDirectionRight) {
        NSLog(@"右滑");
        leftControl.view.hidden = NO;
        if (!_backgroundBtn) {
            _backgroundBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            _backgroundBtn.backgroundColor = [UIColor clearColor];
            _backgroundBtn.frame = mainControl.view.frame;
            [_backgroundBtn addTarget:self action:@selector(backgroundBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            
            
            [mainControl.view addSubview:_backgroundBtn];
        }else{
            _backgroundBtn.hidden = NO;
        }
        [self showLeftView];
        
        
    }

//    //根据视图位置判断是左滑还是右边滑动
//    if (rec.view.frame.origin.x>=0){
//        CGPoint point = [rec translationInView:self.view];
//        scalef = (point.x*speedf+scalef);
//    //    NSLog(@"1 --------- >  %f",rec.view.frame.origin.x);
//        
//        rec.view.center = CGPointMake(rec.view.center.x + point.x*speedf,rec.view.center.y);
//        rec.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,1,1);
//        [rec setTranslation:CGPointMake(0, 0) inView:self.view];
//        
//        righControl.view.hidden = YES;
//        leftControl.view.hidden = NO;
//        
//    }
//    else
//    {
//       //  NSLog(@"2 -------- >  %f",rec.view.frame.origin.x);
////        rec.view.center = CGPointMake(rec.view.center.x ,rec.view.center.y);
////        rec.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,1,1);
////        [rec setTranslation:CGPointMake(0, 0) inView:self.view];
////
////        righControl.view.hidden = NO;
////        leftControl.view.hidden = YES;
////        mainControl.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,1.0,1.0);
////        mainControl.view.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2,[UIScreen mainScreen].bounds.size.height/2);
//
////        scalef = 0;
////        _backgroundBtn.hidden = YES;
//    }
//
//    if (!_backgroundBtn) {
//        _backgroundBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        _backgroundBtn.backgroundColor = [UIColor clearColor];
//        _backgroundBtn.frame = mainControl.view.frame;
//        [_backgroundBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
//        
//        
//        [mainControl.view addSubview:_backgroundBtn];
//    }else{
//        _backgroundBtn.hidden = NO;
//    }
//    
//    //手势结束后修正位置
//    if (rec.state == UIGestureRecognizerStateEnded) {
//        
//        NSLog(@"---------> >%f",speedf);
//        
//        if (scalef>140*speedf){
//            [self showLeftView];
//        }
////        else if (scalef<-140*speedf) {
////         //   [self showRighView];
////             [self showMainView];
////        }
//        else
//        {
//            [self showMainView];
//            scalef = 0;
//            _backgroundBtn.hidden = YES;
//        }
//    }
//    
    
}
- (void)backgroundBtnClick:(UIButton *)sender
{
    NSLog(@"aaa");
    sender.hidden = YES;
    [self showMainView];
}

#pragma mark - 单击手势
//-(void)handeTap:(UITapGestureRecognizer *)tap{
//    
////    if (tap.state == UIGestureRecognizerStateEnded) {
////        [UIView beginAnimations:nil context:nil];
////        tap.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,1.0,1.0);
////        tap.view.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2,[UIScreen mainScreen].bounds.size.height/2);
////        [UIView commitAnimations];
////        scalef = 0;
////
////    }
//    [self showMainView];
//}

#pragma mark - 修改视图位置
//恢复位置
-(void)showMainView{
    [UIView beginAnimations:nil context:nil];
    mainControl.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,1.0,1.0);
    mainControl.view.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2,[UIScreen mainScreen].bounds.size.height/2);
    [UIView commitAnimations];
}

//显示左视图
-(void)showLeftView{
    [UIView beginAnimations:nil context:nil];
    /**
     1 是要显示View 和屏幕的横竖比例
     */
    mainControl.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,1,1);
    /**
     360  表示的是 偏移量
     */
    mainControl.view.center = CGPointMake(500,[UIScreen mainScreen].bounds.size.height/2);
    [UIView commitAnimations];
    

}

//显示右视图
-(void)showRighView{
    
    [UIView beginAnimations:nil context:nil];
    mainControl.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,0.8,0.8);
    mainControl.view.center = CGPointMake(-60,[UIScreen mainScreen].bounds.size.height/2);
    [UIView commitAnimations];
}

-(void)leftClick:(UIButton *)btn{
    [self.navigationController popViewControllerAnimated:YES];
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setObject:@"YES" forKey:@"show"];
    [userDefault synchronize];
}


//#warning 为了界面美观，所以隐藏了状态栏。如果需要显示则去掉此代码
- (BOOL)prefersStatusBarHidden
{
    return NO; //返回NO表示要显示，返回YES将hiden
}

@end
